import {defineConfig} from 'vite';
import react from '@vitejs/plugin-react';
import * as path from 'node:path';
import {Plugin as importToCDN} from 'vite-plugin-cdn-import';
import {SERVER_PORT} from '../constant.ts';

export default defineConfig({
  server: {
    port: 80,
    host: '0.0.0.0',
    open: true,
    cors: true,
    proxy: {
      '/api': {
        target: `http://localhost:${SERVER_PORT}`,
        changeOrigin: true,
      },
    },
  },
  build: {
    outDir: path.join(process.cwd(), './dist/page'),
    rollupOptions: {
      external: ['react', 'react-dom', 'crypto-js'],
    },
  },
  base: './',
  root: __dirname,
  plugins: [react(), importToCDN({
    modules: [
      {
        name: 'crypto-js',
        var: 'CryptoJS',
        path: 'https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.2.0/crypto-js.min.js',
      },
      {
        name: 'react',
        var: 'React',
        path: 'https://cdnjs.cloudflare.com/ajax/libs/react/18.3.1/umd/react.production.min.js',
      }, {
        name: 'react-dom',
        var: 'ReactDOM',
        path: 'https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.3.1/umd/react-dom.production.min.js',
      },
    ],
  })],
  resolve: {
    alias: {
      '@': path.join(__dirname, 'src'),
      'root': process.cwd(),
    },
  },
});